﻿using System;
using NPMonitor.DataModel;
using NPMonitor.Implementation;

namespace NPMonitor.Web.UserControls
{
    public partial class MachineInfo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.PoolDataBind();
            }
            phPopEdit.Visible = false;
        }

        public void PoolDataBind()
        {
            var machinePoolList = StoreManager.Current.MachineStore.GetAllMachinePool();
            this.ddlMachinePool.DataSource = machinePoolList;
            this.ddlMachinePool.Items.Insert(0, "--Select one Pool--");
            this.ddlMachinePool.DataTextField = "PoolName";
            this.ddlMachinePool.DataValueField = "PoolId";
            this.ddlMachinePool.DataBind();
        }


        public _Default DefaultPage
        {
            get
            {
                return this.Page as _Default;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            MachineData md = new MachineData() { 
             MachineId = MachineId,
             MachineName = MachineName,
             PoolId = MachinePoolId,
             MAC = SerialNumber,
             IP = IpAddress,
             Description = Description,            
            };
            StoreManager.Current.MachineStore.EditMachine(md);
            DefaultPage.BindMachineList();
            DefaultPage.ShowSuccessfulDiv();
        }

        public int MachineId
        {
            get { return int.Parse(lblMachineId.Text); }
            set { lblMachineId.Text = value.ToString(); }
        }

        public string MachineName
        {
            get { return lblMachineName.Text; }
            set { lblMachineName.Text = value; }
        }

        public string SerialNumber
        {
            get { return lblSerialNumber.Text; }
            set { lblSerialNumber.Text = value; }
        }

        public int MachinePoolId
        {
            get { return int.Parse(ddlMachinePool.SelectedValue); }
        }

        public string IpAddress
        {
            get { return lblIpAddress.Text; }
            set { lblIpAddress.Text = value; }
        }

        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        public bool PhPopEdit
        {
            set
            {
                phPopEdit.Visible = value;
            }
        }
        public void ShowMachineInfo(int id)
        {
            MachineData md = new MachineData();
            md = StoreManager.Current.MachineStore.GetMachineInfoById(id);
            if (md.PoolId == 0)
            {
                ddlMachinePool.SelectedIndex = 0;
            }
            else
            ddlMachinePool.SelectedValue = md.PoolId.ToString();
            lblMachineName.Text = md.MachineName;
            lblSerialNumber.Text = md.MAC;
            lblIpAddress.Text = md.IP;
            lblMachineId.Text = md.MachineId.ToString();
            txtDescription.Text = md.Description;
            lblStatus.Text = md.Connectivity;
        }

        protected override void OnPreRender(EventArgs e)
        {
            cvDescription.ErrorMessage = string.Format(ValidationString.FieldLengthExceeded, "Description", "1000");
            cvMachinePool.ErrorMessage = string.Format(ValidationString.FieldRequired, "MachinePool");
        }

    }
}